USE [Sikra]
GO

/****** Object:  StoredProcedure [dbo].[SP_UtilityEntityGeneration]    Script Date: 01/02/2012 23:14:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*******************************************************************************************    
*	Type                    :	Procedure    
*	Called by               :	Utility procedure called by other procedure
*	Name                    :	SP_UtilityEntityGeneration
*	Input Parameters        :	@tblName
*	Output Parameters		:	None
*	Sample Calls 			:	EXEC SP_UtilityEntityGeneration '@tblName'							
*	Purpose                 :	For generating get set property of an entity
*	Created Date            :   09-01-2009   
*	Created By              :   Ambarish Roy      
*******************************************************************************************/

ALTER PROCEDURE [dbo].[SP_UtilityEntityGeneration]
AS
BEGIN
	
	DECLARE @tblName VARCHAR(50)
	DECLARE @fldName VARCHAR(50)
	DECLARE @DataType VARCHAR(8000)
	DECLARE @Col_Type INT

	SET @tblName=''
	SET @DataType=''
	SET @fldName=''
	SET @Col_Type=0

    DECLARE cursITEM0 CURSOR FOR 
		SELECT t1.name 
		FROM sysobjects t1
		INNER JOIN sysindexes t2
		ON t1.id = t2.id
			WHERE t2.indid <= 1
			AND OBJECTPROPERTY(t1.id,'IsUserTable') = 1
	OPEN cursITEM0
	FETCH NEXT FROM cursITEM0 INTO @tblName
	WHILE @@FETCH_STATUS = 0
		BEGIN
			PRINT '[Serializable]'
			PRINT 'public partial class '+@tblName
			PRINT '{'
			DECLARE cursITEM1 CURSOR FOR SELECT NAME,system_type_id FROM SYS.COLUMNS  WHERE OBJECT_ID(@tblName) = OBJECT_ID
			OPEN cursITEM1
			FETCH NEXT FROM cursITEM1 INTO @fldName,@Col_Type
				WHILE @@FETCH_STATUS = 0
				BEGIN
					IF @Col_Type=127 --BIGINT 
					BEGIN
						SET @DataType='long'
					END
					ELSE IF @Col_Type=56 --INT 
					BEGIN
						SET @DataType='int'
					END
					ELSE IF @Col_Type=167 --VARCHAR 
					BEGIN
						SET @DataType='string'
					END
					ELSE IF @Col_Type=62 --FLOAT 
					BEGIN
						SET @DataType='double'
					END
					ELSE IF @Col_Type=104 --BIT 
					BEGIN
						SET @DataType='bool'
					END
					ELSE IF @Col_Type=61 --DATETIME 
					BEGIN
						SET @DataType='DateTime'
					END

					PRINT 'private '+@DataType+ ' _'+@fldName+';'
					PRINT 'public '+@DataType+ ' '+ @fldName
					PRINT '{get{return _'+@fldName+';}'
					PRINT 'set{ _'+@fldName+'=value;}}'
					PRINT ''

					FETCH NEXT FROM cursITEM1 INTO @fldName,@Col_Type
				END
			CLOSE cursITEM1
			DEALLOCATE cursITEM1
			PRINT '}'
			PRINT '////////////////////////////////////////////////////////////////////////////'
		END
	CLOSE cursITEM0
	DEALLOCATE cursITEM0
	
END

GO


